package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
@Slf4j
public class ExtCproductController extends BaseController{

    // 注入dubbo的服务接口的代理对象
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 附件列表
     * 功能入口：project-list.jsp货物列表，点击”附件“
     * 请求地址：http://localhost:8080/cargo/extCproduct/list.do
     * 请求参数：
     *      contractProductId  货物列表传入的货物id，用于根据货物id查询附件
     *      contractId         传给附件列表，保存附件时候需要指定购销合同id
     * 响应地址：
     *      /WEB-INF/pages/cargo/extc/extc-list.jsp
     */
    @RequestMapping("/list")
    public String list(String contractId,String contractProductId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {

        //1. 根据货物id查询附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        // select * from co_ext_cproduct where contract_product_id=?
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo =
                extCproductService.findByPage(extCproductExample, pageNum, pageSize);

        //2. 查询附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        //3. 保存
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        // 转发到货物添加、列表页面
        return "cargo/extc/extc-list";
    }

    /**
     * 附件添加、修改
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct) {
        // 设置企业信息
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        // 判断
        if (StringUtils.isEmpty(extCproduct.getId())) {
            extCproductService.save(extCproduct);
        } else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()+
                "&contractProductId="+extCproduct.getContractProductId();
    }

    /**
     * 进入修改页面：
     * 请求地址：http://localhost:8080/cargo/extCproduct/toUpdate.do
     * 请求参数：
     *    id                 附件id
     *    contractProductId  货物id
     *    contractId         购销合同id
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractProductId,String contractId) {
        // 根据附件id查询
        ExtCproduct extCproduct = extCproductService.findById(id);

        //查询货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // 保存
        request.setAttribute("extCproduct", extCproduct);
        request.setAttribute("factoryList", factoryList);
        // 页面取值：${contractProductId}
        request.setAttribute("contractProductId", contractProductId);
        request.setAttribute("contractId", contractId);
        return "cargo/extc/extc-update";
    }

    /**
     * 删除附件
     * 请求地址：http://localhost:8080/cargo/extCproduct/delete.do
     * 请求参数：
     *    id                 附件id
     *    contractProductId  货物id
     *    contractId         购销合同id
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractProductId,String contractId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractId="+contractId+
                "&contractProductId="+contractProductId;
    }
}